package com.gufe.controller.admin.mmp;


import com.gufe.constant.MessageConstant;
import com.gufe.result.Result;
import com.gufe.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/*
* 公共类接口
* */
@Slf4j
@RestController
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
    AliOssUtil aliOssUtil;

    /*上传文件*/
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try {
            log.info("开始上传文件{}",file);
            //获取原始文件名：
            String originalFilename = file.getOriginalFilename();
            //得到后缀
            String extension = originalFilename.toString().substring(originalFilename.lastIndexOf('.'));
            //生成新文件名
            String newName = UUID.randomUUID().toString() + extension;
            String fileUrl = aliOssUtil.upload(file.getBytes(), newName);
            return Result.success(fileUrl);

        } catch (IOException e) {
            log.info("上传文件失败{}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
